let ajax = getApp().globalData.ajax;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {

    },
    categorys: ['绝地求生', '王者荣耀', '情感陪伴', '情感发泄',],
    coupons: [],
    skill: {},
    orderInfo: {
      coupon: '',
      categoryName: '',
      time: '12:01',
      orderNum: 1,
      noteValue: '',
      price: 0,
      total: 0,
    },
    plusIcon: 'http://image.tanwancat.com/ecfae3dbdc89d725b77f41df4792f1e3_36_36.jpeg',
    reduceIcon: 'http://image.tanwancat.com/a3bb75e9dd9da1496eeebd26ebb45fc4_36_36.jpeg',
    arrowSrc: 'https://bpic.588ku.com/element_pic/00/96/10/4456f2f6f075d88.jpg!/fw/254/quality/90/unsharp/true/compress/true'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.getCoupons();
    let skill = JSON.parse(options.skill || "");
    let price = options.price;
    let total = price;
    let { sellerId, avatar, nick, userSkillId } = options;
    let currentDate = new Date()//获取当前时间
    let time = currentDate.getHours() + ':' + (currentDate.getMinutes() < 10 ? '0' + currentDate.getMinutes() : currentDate.getMinutes())//修改时间显示为当前时间
    let orderInfo = Object.assign(this.data.orderInfo, { price, skill, sellerId, total, avatar, nick, userSkillId, time });
    this.setData({
      skill,
      orderInfo,
      time
    })
  },

  //获取优惠券
  getCoupons () {
    ajax('user/coupon', { token: wx.getStorageSync('Cookie'), }).then(res => {
      let coupons = [];
      res.data.forEach(item => {
        coupons.push({ id: item.coupon.id, money: item.coupon.money, coupon_user_id: item.coupon_user_id })
      })
      coupons.unshift({ id: 0, money: 0, coupon_user_id: 0 })


      this.setData({ coupons })

    })
  },

  bindPickerChange (e) {
    let index = e.detail.value;
    let categoryName = this.data.categorys.find((item, ind) => ind == index);
    let orderInfo = Object.assign(this.data.orderInfo, { categoryName });
    this.setData({ orderInfo });
  },

  //选择优惠券
  bindCouponsChange (e) {
    let index = e.detail.value;
    let coupon = this.data.coupons.find((item, ind) => ind == index);

    let total = (parseInt(this.data.orderInfo.price * 100) * this.data.orderInfo.orderNum - parseInt(coupon.money * 100)) / 100;
    let orderInfo = Object.assign(this.data.orderInfo, { coupon, total });

    this.setData({ orderInfo });
  },

  bindTimeChange: function (e) {
    console.log(e);
    let orderInfo = Object.assign(this.data.orderInfo, { time: e.detail.value });
    this.setData({
      orderInfo
    })
  },

  //加时间
  plusOrderNum () {
    let [orderNum, price, total] = [this.data.orderInfo.orderNum, this.data.orderInfo.price, this.data.orderInfo.total];

    orderNum++;
    total = (orderNum * parseInt(price * 100) - parseInt(this.data.orderInfo.coupon.money * 100 || 0)) / 100;

    let orderInfo = Object.assign(this.data.orderInfo, { orderNum, total });
    this.setData({
      orderInfo
    })
  },
  //减时间
  reduceOrderNum () {
    let [orderNum, price, total] = [this.data.orderInfo.orderNum, this.data.orderInfo.price, this.data.orderInfo.total];
    if (orderNum === 1) {
      return;
    } else {
      orderNum--;
      total = (orderNum * parseInt(price * 100) - parseInt(this.data.orderInfo.coupon.money * 100 || 0)) / 100;
      let orderInfo = Object.assign(this.data.orderInfo, { orderNum, total });
      this.setData({
        orderInfo,
      })
    }

  },

  setNoteValue (e) {
    let orderInfo = Object.assign(this.data.orderInfo, { noteValue: e.detail.value });
    this.setData({
      orderInfo
    })
  },
  toOrderDetail () {
    console.log(this.data.orderInfo);
    let json = {
      token: wx.getStorageSync('Cookie'),
      seller_id: this.data.orderInfo.sellerId,
      user_skill_id: this.data.orderInfo.userSkillId,
      plan_time: this.data.orderInfo.time,
      duration: this.data.orderInfo.orderNum,
      price: this.data.orderInfo.total,
      note_value: this.data.orderInfo.noteValue,
      coupon_user_id: this.data.orderInfo.coupon.coupon_user_id || 0,
    }
    ajax('order/create', json).then(res => {
      if (res.code == -1) {
        wx.showToast({
          title: '当前还存在未完成的订单',
          icon: 'none',
          duration: 1000,
          mask: true
        })
        return
      }
      let id = res.data.orders.order_id;
      let json = {
        token: wx.getStorageSync('Cookie'),
        pay_type: 'WEI_XIN',
        order_type: 'Order',
        order_no: res.data.orders.order_no,
      }
      ajax('pay/info', json).then(res => {
        let msg = res.data.msg;
        console.log(res);
        wx.requestPayment({
          timeStamp: msg.timeStamp,
          nonceStr: msg.nonceStr,
          package: msg.package,
          signType: 'MD5',
          paySign: msg.paySign,
          success () {
            wx.navigateTo({
              url: `../orderDetail/orderDetail?id=${id}`,
            })
          },
          fail (err) {
            console.log(err);
          },
          complete () {
            console.log({
              timeStamp: msg.timeStamp,
              nonceStr: msg.nonceStr,
              package: msg.package,
              signType: 'MD5',
              paySign: msg.paySign,
            })
          }
        })


      })


    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})